<?php
/**
 * Description of TipoLeilaoDAO
 *
 * @author Magno
 */
class TipoLeilaoDAO {

    private $conexao;
    public function __construct() {
        $this->conexao = Conexao::getConexao();
    }

    public function salvar($tipoLeilao) {

        try {

            if($tipoLeilao->getTipoLeilaoID() != null && $tipoLeilao->getTipoLeilaoID() > 0) {
                $statment = $this->conexao->prepare("UPDATE tipoLeilao SET
                        descricao = :descricao
                        WHERE tipoLeilaoID = :tipoLeilaoID");

            }else {
                $statment = $this->conexao->prepare("INSERT INTO tipoLeilao(tipoLeilaoID,descricao) VALUES (:tipoLeilaoID,:descricao);");
            }

            if($statment->execute($tipoLeilao->toBD())) {

                if($tipoLeilao->getTipoLeilaoID() == null || $tipoLeilao->getTipoLeilaoID() <= 0)
                    $tipoLeilao->setTipoLeilaoID($this->conexao->lastInsertId());

                return $tipoLeilao;
            }else {
                $erros = $statment->errorInfo();
                throw new Exception("Erro TipoLeilaoDAO: ".$erros[2]);
            }

        }catch(PDOException $err) {
            throw new Exception("Erro TipoLeilaoDAO:".$err->getMessage());
        }

    }

    public function excluir($tipoLeilaoID) {

        try {

            $statment = $this->conexao->prepare("DELETE FROM tipoLeilao WHERE tipoLeilaoID = :tipoLeilaoID");
            $statment->bindParam(':tipoLeilaoID',$tipoLeilaoID);

            if(!$statment->execute()) {
                $erros = $statment->errorInfo();
                throw new Exception("Erro TipoLeilaoDAO: ".$erros[2]);
            }

        }catch(PDOException $err) {
            throw new Exception("Erro TipoLeilaoDAO:".$err->getMessage());
        }

    }

    public function listar($pagina = 0,$descricao = "") {
        try {

            $limite = Util::getPaginacao($pagina);
            $where = $this->getWhere($descricao);
            $tipoLeilaos = array();
            $statment = $this->conexao->query( "SELECT * FROM tipoLeilao e $where $limite" );

            foreach ( $statment->fetchAll(PDO::FETCH_SERIALIZE) as $row )
                $tipoLeilaos[] = new TipoLeilaoBean($row['tipoLeilaoID'], $row['descricao'], $row['sigla']);

            return $tipoLeilaos;

        }catch(PDOException $err) {
            throw new Exception("Erro TipoLeilaoDAO:".$err->getMessage());
        }
    }

    public function buscarPorID($tipoLeilaoID) {
        try {

            $row = $this->conexao->query( "SELECT * FROM tipoLeilao WHERE tipoLeilaoID = $tipoLeilaoID " )->fetch(PDO::FETCH_SERIALIZE);

            if(isset($row['tipoLeilaoID']) && $row['tipoLeilaoID'] > 0) {
                $tipoLeilao = new TipoLeilaoBean($row['tipoLeilaoID'], $row['descricao'], $row['sigla']);
                return $tipoLeilao;
            }else {
                return null;
            }

        }catch(PDOException $err) {
            throw new Exception("Erro TipoLeilaoDAO:".$err->getMessage());
        }
    }

    private function getWhere($descricao = "") {
        $where = " WHERE 1 = 1 ";

        if (isset ($descricao) && strlen($descricao) > 0) {
            $where = $where." AND e.descricao = '$descricao' ";
        }

        return $where;
    }

    public function total($descricao = "") {
        try {

            $where = $this->getWhere($descricao);
            $total = $this->conexao->query( "SELECT COUNT(e.tipoLeilaoID) total FROM tipoLeilao e $where" )->fetch(PDO::FETCH_COLUMN);
            return $total;

        }catch(PDOException $err) {
            throw new Exception("Erro TipoLeilaoDAO:".$err->getMessage());
        }
    }

}
?>
